package com.jchen.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jchen.common.entity.UserInfo;
import com.jchen.common.util.R;
import com.jchen.elasticsearch.dto.SysUserEsQueryDto;
import com.jchen.elasticsearch.dto.SysUserEsQueryReqDto;
import com.jchen.system.entity.SysUserEs;
import com.jchen.system.mapper.UserInfoMapper;
import com.jchen.system.service.IUserInfoService;
import com.jchen.system.service.SysUserSearchService;
import net.bytebuddy.asm.Advice;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @Description
 * @Author jchen
 * @Date 2023/12/7 14:23
 */
@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private SysUserSearchService sysUserSearchService;

    @Override
    public UserInfo getByUsername(String name) {
        UserInfo userInfoQuery = new UserInfo();
        userInfoQuery.setUsername(name);
        UserInfo userInfo = userInfoMapper.selectOne(userInfoQuery);
        return userInfo;
    }

    @Override
    public String saveUserInfo(UserInfo userInfo) {
        int i =userInfoMapper.insertSelective(userInfo);
        if(i>0){
            return "成功";
        }
        return "失败";
    }

    @Override
    public PageInfo<UserInfo> selectListPage(int pageNum, int pageSize, UserInfo userInfo) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserInfo> userInfos = userInfoMapper.select(userInfo);
        PageInfo<UserInfo> page = new PageInfo<>(userInfos);
        return page;
    }

    @Override
    public UserInfo getByUserId(int userId) {
        return userInfoMapper.selectById(userId);
    }

    @Override
    public R listPageByEs(int pageNum, int pageSize, SysUserEsQueryDto dto) throws IOException {
        PageInfo<SysUserEs> pageInfo;
        // elasticsearch查询
        SysUserEsQueryReqDto searchReq = new SysUserEsQueryReqDto();
        searchReq.setName(dto.getName());
        searchReq.setStartAge(dto.getStartAge());
        searchReq.setEndAge(dto.getEndAge());
        pageInfo = sysUserSearchService.search(pageNum, pageSize, searchReq, SysUserEs.class);
        return R.ok(pageInfo);
    }
}
